package com.dxy.df.modules.controller;

import com.dxy.df.core.assit.SearchHandler;
import com.dxy.df.core.model.PageRs;
import com.dxy.df.core.model.Rs;
import com.dxy.df.core.shiro.ShiroUtils;
import com.dxy.df.modules.application.GivenRiceApplication;
import com.dxy.df.modules.bean.put.UserPublishGiveRicePut;
import com.dxy.df.modules.bean.query.UserPublishGiveRiceQuery;
import com.dxy.df.modules.bean.vo.UserPublishGiveRiceVo;
import com.dxy.df.modules.service.UserPublishGiveRiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 用户出饭信息发布
 *
 * @author biz@dxy.cn
 * @date 2019-10-25 15:36:06
 */
@Api(tags = "用户出饭信息发布")
@RestController
@RequestMapping("/api/users/publish/given-rice")
public class UserPublishGiveRiceController {
    @Autowired
    private UserPublishGiveRiceService userPublishGiveRiceService;
    @Autowired
    private SearchHandler searchHandler;
    @Autowired
    private GivenRiceApplication givenRiceApplication;

    @ApiOperation(value = "用户出饭信息发布分页列表")
    @PostMapping("/search")
    public PageRs<UserPublishGiveRiceVo> search(@RequestBody @Valid UserPublishGiveRiceQuery query) {
        //查询列表数据
        query.setPublishTime(new Date());
        query.setBizStatus(0);
        List<UserPublishGiveRiceVo> voList = userPublishGiveRiceService.queryList(query);
        int total = userPublishGiveRiceService.queryTotal(query);
        return searchHandler.search(voList, total, query);
    }


    @ApiOperation(value = "用户出饭信息发布信息")
    @GetMapping()
    public Rs<UserPublishGiveRiceVo> info() {
        UserPublishGiveRiceQuery query = new UserPublishGiveRiceQuery();
        query.setPage(1);
        query.setLimit(1);
        query.setOffset(0);
        query.setPublishTime(new Date());
        query.setUserId(ShiroUtils.getUserAccount());
        List<UserPublishGiveRiceVo> userPublishGiveRiceVos = userPublishGiveRiceService.queryList(query);
        if (null != userPublishGiveRiceVos && !userPublishGiveRiceVos.isEmpty()) {
            return Rs.ok().putData(userPublishGiveRiceVos.get(0));
        }
        UserPublishGiveRiceVo vo = new UserPublishGiveRiceVo();
        vo.setBizStatus(0);
        return Rs.ok().putData(vo);
    }

    @ApiOperation(value = "用户出饭信息发布新增")
    @PostMapping
    public Rs save(@RequestBody UserPublishGiveRicePut userPublishGiveRice) {
        givenRiceApplication.saveUserPublishGivenInfo(userPublishGiveRice);
        return Rs.ok();
    }

    @ApiOperation(value = "用户出饭信息发布修改")
    @PutMapping
    public Rs update(@RequestBody UserPublishGiveRicePut userPublishGiveRice) {
        userPublishGiveRiceService.update(userPublishGiveRice);
        return Rs.ok();
    }

    @ApiOperation(value = "用户出饭信息发布删除")
    @DeleteMapping
    public Rs delete(@RequestParam Integer[] ids) {
        userPublishGiveRiceService.deleteBatch(ids);
        return Rs.ok();
    }

}
